package com.wqiuqiu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.wqiuqiu.common.PageUtils;
import com.wqiuqiu.common.R;
import com.wqiuqiu.gulimall.product.entity.ProductAttrValueEntity;
import com.wqiuqiu.gulimall.product.service.ProductAttrValueService;
import com.wqiuqiu.gulimall.product.vo.AttrResVo;
import com.wqiuqiu.gulimall.product.vo.AttrVo;
import com.wqiuqiu.gulimall.product.vo.SpuAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wqiuqiu.gulimall.product.entity.AttrEntity;
import com.wqiuqiu.gulimall.product.service.AttrService;



/**
 * 商品属性
 *
 * @author wqiuqiu
 * @email 18773489795@163.com
 * @date 2023-12-11 12:32:41
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 基本属性列表
     */
    @GetMapping("/{type}/list/{categoryId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,@PathVariable("type") String type,
                          @PathVariable("categoryId") Long categoryId){
        PageUtils page = attrService.queryAttrPage(params,categoryId,type);

        return R.ok().put("page", page);
    }
    /**
     * 获取spu规格
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R saleAttrList(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> data = attrService.getSpuAttrInfoBySpuId(spuId);

        return R.ok().put("data", data);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrResVo attrResVo = attrService.getAttrById(attrId);

        return R.ok().put("attr", attrResVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttrVo(attr);

        return R.ok();
    }
    /**
     * 修改商品规格
     */
    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@RequestBody List<SpuAttrVo> attr,@PathVariable("spuId") Long spuId){
        productAttrValueService.updateSpuAttr(attr,spuId);

        return R.ok();
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
